package com.intramirror.product.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author lei
 * 2011-9-2
 */
public class StringUtils {
 
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
    
    /**
     * @param str 字符串
     * @return
     * 是否为空串
     */
    public static boolean isNotEmpty(String str){
    	if(str == null || str.isEmpty()){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @param str 字符串
     * @return
     * 是否为空串
     */
    public static boolean isEmpty(String str){
    	if(str == null || str.isEmpty()){
    		return true;
    	}
    	return false;
    }
    
    /**
     * @param str 字符串
     * @param spiltChar 
     * @return
     */
    public static List<Long> spiltString(String str,String spiltChar){
    	List<Long> list = new ArrayList<>();
    	if(!StringUtils.isNotEmpty(str)){
    		return null;
    	}
    	String[] arr = str.split(spiltChar);
    	for(String s:arr){
    		list.add(Long.valueOf(s));
    	}
    	return list;
    }
    
    /**
	 * @param list 列表
	 * @param c 字符
	 * @return
	 */
	public static<T> String contactToString(List<T> list,String c){
		if(list ==null ||list.size()==0){
			return null;
		}
		StringBuffer stringBuffer = new StringBuffer("");
		for(int i=0;i<list.size();i++){
			if(i==(list.size()-1)){
				stringBuffer.append(list.get(i));
			}else{
				stringBuffer.append(list.get(i));
				stringBuffer.append(c);
			}
		}
		return stringBuffer.toString();
	}
    /*-----------------------------------
 
    笨方法：String s = "你要去除的字符串";
 
            1.去除空格：s = s.replace('\\s','');
 
            2.去除回车：s = s.replace('\n','');
 
    这样也可以把空格和回车去掉，其他也可以照这样做。
 
    注：\n 回车(\u000a) 
    \t 水平制表符(\u0009) 
    \s 空格(\u0008) 
    \r 换行(\u000d)*/
	
	  public static String getExceptionMessage(Throwable e){
    	try {
    		StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            String string = sw.toString();
            String replaceAll = string.replaceAll("\t","");
            return replaceAll;
		} catch (Exception e2) {
			return e2.toString();
		}
	}
	
	/**
	 * 连接成字符串
	 *
	 * @param obj
	 * @return
	 */
	public static String appendString(Object... obj) {
		StringBuilder sb = new StringBuilder();
		if (obj.length == 0) {
			return sb.toString();
		}
		for (int i = 0; i < obj.length; i++) {
			if (obj[i] != null) {
				sb.append(obj[i]);
			}
		}
		return sb.toString();
	}

	public static String objToString(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return null;
	}
	
}